package com.wlyuan.index.remote.consumer;

import com.wlyuan.index.api.IOrderIndexService;
import com.wlyuan.index.remote.message.OrderMessage;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author yuanjie
 */
@Slf4j
@Component
public class OrderMessageConsumer extends AbstractMessageConsumer<OrderMessage> {
    @Resource
    private IOrderIndexService orderIndexService;

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = Queues.ORDER),
            exchange = @Exchange(value = Exchanges.ORDER),
            key = {
                    "OrderCreated", "OrderSigned", "OrderReceipted",
                    "OrderClosed", "OrderScored", "OrderSignPatched",
                    "OrderRemarked", "OrderReceiptLost", "OrderUpdated"
            })
    }, concurrency = "4")
    public void consume(String string) {
        if (logger.isDebugEnabled()) {
            logger.debug("收到订单消息: {}", string);
        }
        try {
            this.handle(string, OrderMessage.class, message -> {
                var order = message.getOrder();
                orderIndexService.index(order);
            });
        } catch (Exception e) {
            logger.error("处理订单消息失败：{}", string, e);
        }
    }
}
